using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using TEngine;
using UnityEngine;
using static UnityEngine.ParticleSystem;

namespace GameLogic
{
    public class EffectCom : MonoBehaviour
    {
        [SerializeField]
        Vector3 m_Offset;

        [SerializeField]
        public float m_FlyToTargetDelay = 1;

        [SerializeField]
        public Vector3 m_TargetPosition;
        [SerializeField]
        public bool m_HasTarget = false;
        [SerializeField]
        public string m_RenderLayer = "Top";
        bool m_IsFlyToTarget = false;

        void Start()
        {
        
        }

        // Update is called once per frame
        void Update()
        {
            if(m_IsFlyToTarget)
            {
                var particles = transform.GetComponentsInChildren<ParticleSystem>();
                for (var i = 0; i < particles.Length; ++i)
                {
                    UpdateParticlePositionToTarget(particles[i]);
                }
                UpdateParticlePositionToTarget(gameObject.GetComponent<ParticleSystem>());
            }
        }
        void UpdateParticlePositionToTarget(ParticleSystem particleSys)
        {
            if (!particleSys)
                return;
            Particle[] particles = new Particle[particleSys.particleCount];
            particleSys.GetParticles(particles);
            for (var i = 0; i < particles.Length; ++i)
            {
                particles[i].position = Vector3.Lerp(particles[i].position, m_TargetPosition, 8 * Time.deltaTime);
            }
            particleSys.SetParticles(particles);
        }

        public void Play()
        {
            var effectDuration = GetEffectsDuration(gameObject, 0);
            DOVirtual.DelayedCall(effectDuration, () => {
                    PoolManager.Instance.PushGameObject(gameObject);
                m_HasTarget = false;
            });
            if(m_HasTarget)
            {
                DOVirtual.DelayedCall(m_FlyToTargetDelay, () =>
                {
                    FlyToTarget();
                });
            }
        }
        void FlyToTarget()
        {
            m_IsFlyToTarget = true;
        }

        float GetEffectsDuration(GameObject obj, float duration)
        {
            obj.layer = LayerMask.NameToLayer("UI");
            for (int i = 0; i< obj.transform.childCount; i++)
            {
                var childTr = obj.transform.GetChild(i);
                duration = GetEffectsDuration(childTr.gameObject, duration);
            }
            var particleSystem = obj.GetComponent<ParticleSystem>();
            if (!particleSystem)
                return duration;
            ParticleSystemRenderer renderer = particleSystem.GetComponent<ParticleSystemRenderer>();
            renderer.sortingLayerID = SortingLayer.NameToID(m_RenderLayer);
            if (duration < particleSystem.main.duration)
                duration = particleSystem.main.duration;

            return duration;
        }
    }
}
